

import eu.jpereira.trainings.designpatterns.structural.adapter.exceptions.CodeMismatchException;
import eu.jpereira.trainings.designpatterns.structural.adapter.exceptions.IncorrectDoorCodeException;
import eu.jpereira.trainings.designpatterns.structural.adapter.model.Door;
import eu.jpereira.trainings.designpatterns.structural.adapter.thirdparty.exceptions.CannotChangeCodeForUnlockedDoor;
import eu.jpereira.trainings.designpatterns.structural.adapter.thirdparty.exceptions.CannotUnlockDoorException;

public class ThirdPartyDoorAdaper extends ThirdPartyDoor implements Door{

	@Override
	public void open(String code) throws IncorrectDoorCodeException {
		try {
			unlock(code);
		} catch (CannotUnlockDoorException e) {
			throw new IncorrectDoorCodeException();
		}
		
	}

	@Override
	public void close() {
		lock();
		
	}

	@Override
	public boolean isOpen() {
		if(getLockStatus() == LockStatus.UNLOCKED) return true;
		else return false;
	}

	@Override
	public void changeCode(String oldCode, String newCode, String newCodeConfirmation) throws IncorrectDoorCodeException, CodeMismatchException {
		open(oldCode);
		if(newCode != newCodeConfirmation){
			throw new CodeMismatchException();
		}
		try {
			setNewLockCode(newCode);
		} catch (CannotChangeCodeForUnlockedDoor e) {
			
		}
		
	}

	@Override
	public boolean testCode(String code) {
		try {
			open(code);
		} catch (IncorrectDoorCodeException e) {
			return false;
		}
		close();
		return true;
	}

}
